package beans;

import java.util.ArrayList;

import javax.ejb.Stateless;

import db.DBBookManager;
import db.DBLocationManager;
import db.DBUserManager;
import db.DbRelationshipManager;
import db.DbRequestManager;

import entities.*;

@Stateless(mappedName = "BookManager")
public class BookManagerBean implements BookManager {

	public String addRequest(Integer[] id, int userId, Integer[] ownerId) {
		String message = "";
		ArrayList<Integer> failedId = DbRequestManager.addRequest(id, userId,
				ownerId);
		if (failedId == null || failedId.size() == 0) {
			message = "success";
		} else {
			message = "Request of\n";
			for (int i = 0; i < failedId.size(); i++) {
				Item u = DBBookManager.lookupCopy(failedId.get(i).intValue());
				String name = "-" + u.getTitle() + "\n";
				message += name;
			}
			message += "failed";
		}
		return message;
	}

	@Override
	public boolean addCopy(Item b) {
		// TODO Auto-generated method stub
		System.out.println("Getting location");
		int locId = DBLocationManager.getLocationID(b.getLocation());
		if (locId < 0) {
			if (!DBLocationManager.addLocation(b.getLocation()))
				return false;
			locId = DBLocationManager.getLocationID(b.getLocation());
			if (locId < 0)
				return false;
		}
		System.out.println("Got Location id: " + locId);
		b.setLocationId(locId);

		b.setCopyStatus(Item.inLib);
		if (b.getVisible() == null)
			b.setVisible(Item.PUBLIC);

		// get master record
		System.out.println("Getting Master id");
		int masterId = DBBookManager
				.findMasterRecord(b.getTitle(), b.getType());
		if (masterId < 0) {
			masterId = DBBookManager.addMasterRecord(b.getTitle(), b.getType());
			if (masterId < 0)
				return false;
		}
		b.setMasterId(masterId);
		System.out.println("Got Master id: " + masterId);
		return DBBookManager.addCopy(b);

	}

	@Override
	public int addMasterRecord(String title, String type) {
		// TODO Auto-generated method stub
		if (!type.equals(Item.BOOK) && !type.equals(Item.CD)
				&& !type.equals(Item.CD))
			return -1; // error
		return DBBookManager.addMasterRecord(title, type);
	}

	@Override
	public boolean deleteCopy(int copyId) {
		// TODO Auto-generated method stub
		DbRequestManager.deleteCopyRequest(copyId); 
		if ( !(DBBookManager.deleteCopy(copyId)) )
			return false;
		return true;
	}

	@Override
	public boolean deleteMasterRecord(String title, String type) {
		// TODO Auto-generated method stub
		return DBBookManager.deleteMasterRecord(title, type);
	}

	@Override
	public ArrayList<Item> getCopies(int masterId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int lookupMasterRecord(String name) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int lookupMasterRecord(String name, String type) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Item getCopy(int copyId) {
		// TODO Auto-generated method stub
		Item book = DBBookManager.lookupCopy(copyId);
		return book;
	}

	@Override
	public boolean updateCopy(Item item) {
		int locId = DBLocationManager.getLocationID(item.getLocation());
		if (locId < 0) {
			if (!DBLocationManager.addLocation(item.getLocation()))
				return false;
			locId = DBLocationManager.getLocationID(item.getLocation());
			if (locId < 0)
				return false;
		}
		System.out.println("Got Location id: " + locId);
		item.setLocationId(locId);

		// get master record
		System.out.println("Getting Master id");
		int masterId = DBBookManager
				.findMasterRecord(item.getTitle(), item.getType());
		if (masterId < 0) {
			masterId = DBBookManager.addMasterRecord(item.getTitle(), item.getType());
			if (masterId < 0)
				return false;
		}
		item.setMasterId(masterId);
		System.out.println("Got Master id: " + masterId);
		// TODO Auto-generated method stub
		return DBBookManager.updateCopy(item);
	}


	@Override
	public Boolean updateMasterRecord(Item item) {
		// TODO Auto-generated method stub
		return DBBookManager.updateMasterRecord(item);
	}

}
